﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JeanFish.Common;

namespace JeanFish.UnitTest.Common
{
    [TestClass]
    public class DateTimeHelperTest
    {
        [TestMethod]
        public void Iso8601DateTimeTest()
        {
            long ticks=9923982838000L;
            DateTime dt0 = new DateTime(ticks, DateTimeKind.Unspecified);
            string iso8601_0 = dt0.ToIso8601String();
            string actual_iso8601_0 = "0001-01-12T11:39:58.2838";
            Assert.AreEqual(actual_iso8601_0, iso8601_0);

            DateTime dt1 = new DateTime(ticks, DateTimeKind.Utc);
            string iso8601_1 = dt1.ToIso8601String();
            string actual_iso8601_1="0001-01-12T11:39:58.2838Z";
            Assert.AreEqual(actual_iso8601_1, iso8601_1);

            DateTime dt2 = new DateTime(ticks, DateTimeKind.Local);
            string iso8601_2 = dt2.ToIso8601String();
            string actual_iso8601_2 = "0001-01-12T11:39:58.2838+08:00";
            Assert.AreEqual(actual_iso8601_2, iso8601_2);

            DateTime dt;
            bool result = actual_iso8601_0.TryParseIso8601(out dt);
            Assert.IsTrue(result);
            Assert.AreEqual(DateTimeKind.Unspecified, dt.Kind);
            Assert.AreEqual(dt0, dt);

            result = actual_iso8601_1.TryParseIso8601(out dt);
            Assert.IsTrue(result);
            Assert.AreEqual(DateTimeKind.Utc, dt.Kind);
            Assert.AreEqual(dt1, dt);

            result = actual_iso8601_2.TryParseIso8601(out dt,DateTimeKind.Local);
            Assert.IsTrue(result);
            Assert.AreEqual(DateTimeKind.Local, dt.Kind);
            Assert.AreEqual(dt2, dt);

            result = actual_iso8601_2.TryParseIso8601(out dt);
            Assert.IsTrue(result);
            Assert.AreEqual(DateTimeKind.Utc, dt.Kind);
            Assert.AreEqual(dt2.ToUniversalTime(), dt);
        }
    }
}
